package first.endtoend.tsm;

import org.apache.log4j.Logger;
import org.ksoap2.HeaderProperty;
import org.mbds.wolf.tsm.GpTsmController;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.GpFunctions;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.ServiceIdentifierType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.CheckMobileSubscriptionEligibilityRequestType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.DeclareServiceInstanceReferenceRequestType;
import org.mbds.wolf.tsm.gp.systems.messaging2_1_0.ksoap2.services.types.requests.HandleStartServiceStateChangeNotificationRequestType;

import java.io.IOException;
import java.math.BigInteger;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ncordina on 15/06/2015.
 * This Abstract class must be extend to all webservice.
 */
public abstract class TSMHelper
{
    protected static Logger logger = Logger.getLogger(TSMHelper.class.getName());
    protected GpTsmController tsmController;
    protected TSMHelperTools tools = new TSMHelperTools();
    protected String TAG = "PDS";
    protected BigInteger validityPeriod = new BigInteger("120000");
    protected List<HeaderProperty> headers = new ArrayList<HeaderProperty>();


    /**
     * Class Constructor
     * Take the URL of SPTSM Server in parameter and instanciate the CallBack and the controller of Wolf library
     *
     * @param URL
     */
    public TSMHelper(String URL)
    {
        try
        {

            tsmController = new GpTsmController(URL, headers);
        } catch (Exception e)
        {
            System.out.println("CONNECTION ERROR");
            e.printStackTrace();
        }
    }

    public static Logger getLogger()
    {
        return logger;
    }

    public TSMHelperTools getTools()
    {
        return tools;
    }

    public String getTAG()
    {
        return TAG;
    }

    public BigInteger getValidityPeriod()
    {
        return validityPeriod;
    }

    public GpTsmController getTsmController()
    {
        return tsmController;
    }

    public void setTsmController(GpTsmController tsmController)
    {
        this.tsmController = tsmController;
    }

    /**
     * Call DeclareServiceInstanceReference webservice from SPTSM
     * This function create all mandatory parameters for the SOAP webservice
     * This function is mandatory for Suspension UseCase
     *
     * @param serviceIdentifier
     */
    private void declareServiceInstanceReferenceRequest(ServiceIdentifierType serviceIdentifier)
    {
        DeclareServiceInstanceReferenceRequestType requestType = new DeclareServiceInstanceReferenceRequestType(); //Request Creation

        requestType.FunctionCallIdentifier = GpFunctions.DeclareServiceInstanceReference.getFunction();
        requestType.ValidityPeriod = this.validityPeriod; //60s validity period
        requestType.Service = tools.initService();
        requestType.MobileSubscription = tools.initMobileSubcription();
        requestType.SecureElement = tools.initSecureElement();
        requestType.ServiceQualifier = tools.initServiceQualifier();

        //  tsmController.DeclareServiceInstanceReference(callBack,requestType); //Call the webservice

    }


    //TODO Se renseigner sur ce qu'attend les methodes, Paramtre etc..

    /**
     * Call handleStartServiceStateChangeNotification webservice from SPTSM
     * Only Operation is Mandatory
     * This function is mandatory for Suspension UseCase
     *
     * @param operation BigInteger
     *                  1 ? Service deployment
     *                  101 ? Service redeployment
     *                  102 ? Secure Element applications redeployment
     *                  103 ? Device Applications redeployment
     *                  2 ? Service upgrade
     *                  3 ? Service update
     *                  4 ? Service suspension
     *                  5 ? Service resumption
     *                  6 ? Service termination
     */
    private void handleStartServiceStateChangeNotificationRequest(BigInteger operation)
    {
        HandleStartServiceStateChangeNotificationRequestType requestType = new HandleStartServiceStateChangeNotificationRequestType();
        requestType.FunctionCallIdentifier = GpFunctions.HandleStartServiceStateChangeNotification.getFunction();

        requestType.ValidityPeriod = this.validityPeriod; //60s validity period
        requestType.MobileSubscription = tools.initMobileSubcription();
        requestType.SecureElement = tools.initSecureElement();
        requestType.Operation = operation;

        //  tsmController.HandleStartServiceStateChangeNotification(callBack, requestType);
    }

    /**
     *
     */
    //TODO Il y a un parametre MOBILE, pas trouve la reference.... A chercher
    private void CheckMobileSubscriptionEligibilityRequest(ServiceIdentifierType serviceIdentifier)
    {
        CheckMobileSubscriptionEligibilityRequestType requestType = new CheckMobileSubscriptionEligibilityRequestType();
        requestType.FunctionCallIdentifier = GpFunctions.CheckMobileSubscriptionEligibility.getFunction();

        // tsmController.CheckMobileSubscriptionEligibility(callBack,requestType);
    }

    //TODO regrouper toute les méthodes du use case deletion/suspension dans cette méthode.

    /**
     * This method gather all webservice call by the delettion/Suspension use case.
     *
     * @param serviceIdentifier Service Identifier
     * @param operation         Operation
     */
    public void deletationUseCaseRequest(ServiceIdentifierType serviceIdentifier, BigInteger operation)
    {
        this.declareServiceInstanceReferenceRequest(serviceIdentifier);
        this.handleStartServiceStateChangeNotificationRequest(operation);
    }



}
